<template>
	<view class="goods-detail-container">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,i) in pics" :key='i' class="swiperitem">
          <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    
    <!-- 商品信息区 -->
    <view class="info">
      <!-- price -->
      <view class="price">
        ￥{{detailList.goods_price}}
      </view>
      <!-- content -->
      <view class="content">
        <view class="left">
          {{detailList.goods_name}}
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      
      <!-- 运输 -->
      <view class="yf">
        运费：免运费 {{cart.length}}
      </view>
    </view>
    
    <!-- 商品的富文本介绍区 -->
    <rich-text :nodes="detailList.goods_introduce"></rich-text>
    
    <!-- 商品导航组件 -->
    <view class="goods-nav">
          <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
    </view>

	</view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				detailList:{},
        pics:[],
        options:[
          {
            icon:'shop',
            text:'商铺'
          },
          {
            icon:'cart',
            text:'购物车',
            info:2
          }
        ],
        buttonGroup:[
          {
            text:'加入购物车',
            backgroundColor:'#ff0000',
            color:'#fff'
          },
          {
            backgroundColor:'#ffa200',
            text:"立即购买",
            color:"#fff"
          }
        ]
			}
		},
    // 计算属性
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },
    
    // 定义侦听器total,监听total属性的变化
    watch:{
      // total(e){
      //   // 通过数组的find方法找到购物车的对象
      //   const findResult = this.options.find(x.text === '购物车')
      //   if(findResult){
      //     // 动态为购物车设置info属性
      //     findResult.info = e
      //   }
      // }
        // 优化total侦听器
        // 顶一个total属性
        total:{
          // handler属性用来定义侦听器的处理函数
          handler(e){
            const findResult = this.options.find(x => x.text === '购物车')
            if(findResult){
              findResult.info = e
            }
          },
          // immediate属性用来声明页面初次加载时是否会立即调用侦听器
          immediate:true
        }
    },
    onLoad(options) {
      console.log(options)
      this.getGoodsDetail(options)
    },
    
    methods:{
      ...mapMutations('m_cart',['addToCart']),
      
      async getGoodsDetail(e){
        const {data : res} = await uni.$http.get('/goods/detail',e)
        if(res.meta.status !== 200) return uni.$showMsg()
     res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;"').replace(/webp/g, 'jpg')
        this.detailList = res.message
        this.pics = res.message.pics
      },
      
      preview(i){
        uni.previewImage({
          current:i,
          urls:this.pics.map(x => x.pics_big)
        })
      },
      onClick(e){
        if(e.content.icon === 'cart')
        uni.switchTab({
          url:'/pages/cart/cart'
        })
      },
      
      // 点击了加入购物车
      buttonClick(e){
        if(e.content.text === '加入购物车'){
          const goods = {
            goods_id : this.detailList.goods_id,
            goods_price : this.detailList.goods_price,
            goods_name : this.detailList.goods_name,
            goods_count : 1,
            goods_state : true,
            goods_small_logo : this.detailList.goods_small_logo
          }
          this.addToCart(goods)
      }
      }
      
    }
	}
</script>

<style lang="scss">
.goods-detail-container{
  padding-bottom:50px;
  swiper{
    height: 750rpx;
    image{
      width: 100%;
      height: 100%;
    }
  }
  .info{
    .price{
      color: red;
      padding: 10px;
      font-size: 20px;
    }
    
    .content{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left{
        width: 80%;
        border-right: 1px solid #efefef;
        padding: 0 10px;
      }
      .right{
        display: flex;
        flex-direction: column;
        color: gray;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        padding: 10px;
      }
    }
    
    .yf{
      padding: 10px;
      color: gray;
    }
  }
  .goods-nav{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>
